<!DOCTYPE html>
<html>

<body>

    <h1>this关键字在函数中的应用</h1>
    <script>
        /* 1.x将成为window对象 */
       var x = myFunction();           
       
        function myFunction() {
        return this;
        }
        
        console.log(x);
		
		/*2.this为函数fullName'拥有者'myObject对象*/
		var myObject = {
		firstName:"Bill",
		lastName: "Gates",
		fullName: function () {
			return this.firstName + " " + this.lastName;
		}
		}
		console.log(myObject.fullName());         // 将返回 "Bill Gates"
		
		/*3.返回的this是 myObject2*/
		var myObject2 = {
			firstName:"Bill",
			lastName: "Gates",
			fullName: function () {
				return this;
			}
		}
		console.log(myObject2.fullName().firstName);	 // 将返回 "Bill"
		
		/*
			构造器调用会创建新对象。新对象会从其构造器继承属性和方法。构造器内的 this 关键词没有值。

			this 的值会成为调用函数时创建的新对象。
		*/
		function myFunction2(arg1, arg2) {
		  this.firstName = arg1;
		  this.lastName  = arg2;
		}

		var x = new myFunction2("Bill","Gates");
		var y = new myFunction2("Bill","Gates");
		console.log(x == y); 	//输出 false
    </script>
</body>

</html>